package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("ProducerController")
public class ProducerController extends CmiEmaController {

	@RequestMapping("/listProducers")
	public ModelAndView listProducers(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = producerPaginationService.updatePagination(request, pagination);
		Set<Producer> producers = producerPaginationService.getPaginationProducers(pagination);

		mav.addObject("producers", producers);
		mav.addObject("entityName", "producer");
		mav.addObject("pagination", pagination);
		mav.setViewName("producer/listProducers.jsp");
		return mav;
	}

	public String listProducers() {
		return "redirect:/listProducers";
	}

	@RequestMapping("/viewProducer")
	public ModelAndView viewProducer(@RequestParam Integer producerId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("producer", producerDAO.findProducerByPrimaryKey(producerId));
		mav.setViewName("producer/viewProducer.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteProducers")
	public ModelAndView confirmDeleteProducers(
			@RequestParam(value = "deleteProducersIds", required = true) Set<Integer> deleteProducersIds) {
		ModelAndView mav = new ModelAndView();

		Set<Producer> deleteProducers = producerDAO.findProducersByIds(deleteProducersIds);
		Set<Producer> referencedProducers = producerService.loadReferenceProducers(deleteProducers);
		boolean hasReferenced;
		if (referencedProducers.size() == 0) {
			mav.addObject("deleteEntitys", deleteProducers);
			hasReferenced = false;
		} else {
			deleteProducers.removeAll(referencedProducers);
			mav.addObject("referencedEntitys", referencedProducers);
			mav.addObject("deleteEntitys", deleteProducers);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Producer");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listProducers");
		return mav;
	}

	@RequestMapping("/deleteProducer")
	public String deleteProducer(@RequestParam Integer producerId) {
		Producer producer = producerDAO.findProducerByPrimaryKey(producerId);
		producerService.deleteProducer(producer);
		return "forward:/listProducers";
	}

	@RequestMapping("/confirmDeleteProducer")
	public ModelAndView confirmDeleteProducer(@RequestParam Integer producerId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("producer", producerDAO.findProducerByPrimaryKey(producerId));

		mav.setViewName("producer/deleteProducer.jsp");

		return mav;
	}

	@RequestMapping("/deleteProducers")
	public String deleteProducers(
			@RequestParam(value = "deleteProducersId", required = true) Set<Integer> deleteProducersId) {
		producerService.deleteProducers(deleteProducersId);
		return "forward:/listProducers";
	}

	@RequestMapping("/editProducer")
	public ModelAndView editProducer(@RequestParam Integer producerId) {

		ModelAndView mav = new ModelAndView();

		Producer producer = producerDAO.findProducerByPrimaryKey(producerId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("producer", producer);

		// 把所有的引用producer对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(producerService.loadProducerObjectReferencedSet(producer));

		// 将编辑页面producer添加到mav中
		mav.setViewName("producer/editProducer.jsp");
		return mav;
	}

	@RequestMapping("/newProducer")
	public ModelAndView newProducer() {
		ModelAndView mav = new ModelAndView();
		Producer producer = new Producer();

		mav.addObject("producer", producer);

		// 把所有的引用producer对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(producerService.loadProducerObjectReferencedSet(producer));

		mav.addObject("newFlag", true);
		mav.setViewName("producer/editProducer.jsp");

		return mav;
	}

	@RequestMapping("/saveProducer")
	public ModelAndView saveProducer(@ModelAttribute Producer producer) {

		ModelAndView mav = new ModelAndView();

		Address addressTemp = producer.getAddress();
		Address address = addressTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(addressDAO.findAddressById(addressTemp.getId()));

		Producer existingProducer = CmiHibernateProxyUtil
				.deproxy(producerDAO.findProducerByPhysicalPrimaryKey(producer.getName(), address.getId()));

		producer.setAddress(address);
		// 新建记录
		if (producer.getId() == null) {
			if (producer.equals(existingProducer)) {

				mav.addAllObjects(producerService.loadProducerClassReferencedSet(producer));
				// mav = editContinue(mav, producer);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("producer/editProducer.jsp");
			} else {
				producerService.saveProducer(producer);
				mav.setViewName("forward:/listProducers");
			}
		}
		// 编辑记录
		else {
			Producer originProducer = CmiHibernateProxyUtil
					.deproxy(producerDAO.findProducerByPrimaryKey(producer.getId()));
			if (!producer.equals(originProducer) && producer.equals(existingProducer)) {
				mav.addAllObjects(producerService.loadProducerClassReferencedSet(producer));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("producer/editProducer.jsp");
			} else {
				producerService.saveProducer(producer);
				mav.setViewName("forward:/listProducers");
			}
		}
		return mav;
	}

	@RequestMapping("/producerController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
